package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

public class GetAllItemsByCategoryForNewRecipe {
	
	private String category;
	
	public GetAllItemsByCategoryForNewRecipe(String cat)
	{
		this.category=cat;
		
	}
	
	
	public Vector<Vector> getAllItems ()
	{
		Vector<Vector> allItems =new Vector<Vector>();		
        SQLConnection sc=new SQLConnection();
		
		
        if (category.equals("כל המוצרים"))
        {
        	try {			
        		PreparedStatement ps = sc.getConnection().prepareStatement("SELECT it.Iid ,it.Iname,it.ICategory,it.IExpiration,it.IMin_quantity,it.IMax_quantity,it.IUnit_of_measure FROM item it  ;");	

        		
        		
        		ResultSet rs=ps.executeQuery();	

        		ResultSetMetaData rsmd = rs.getMetaData();
        		
        		 while(rs.next())
        	        {   

        			 Vector<String>  row = new Vector<String>();
        	         for(int col = 1; col <= rsmd.getColumnCount(); col++) 
        	         	{                      	                	               
        	                 row.add(rs.getString(col));
        	         	}//for
        	         allItems.add(row);
        	        }//while
        		 ps.close();
        		 rs.close();
        		 sc.getConnection().close();
        		
        		} catch (SQLException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		}
       
        	return allItems;	
        	
        }
        
        else
        {
        
		try {			
		PreparedStatement ps = sc.getConnection().prepareStatement("SELECT it.Iid ,it.Iname,it.ICategory,it.IExpiration,it.IMin_quantity,it.IMax_quantity,it.IUnit_of_measure FROM item it WHERE it.ICategory=?;");	

		
		ps.setString(1, category);
		

		ResultSet rs=ps.executeQuery();	

		ResultSetMetaData rsmd = rs.getMetaData();
		
		 while(rs.next())
	        {   

			 Vector<String>  row = new Vector<String>();
	         for(int col = 1; col <= rsmd.getColumnCount(); col++) 
	         	{                      	                	               
	                 row.add(rs.getString(col));
	                 System.out.println(row);
	         	}//for
	         allItems.add(row);
	        }//while
		 ps.close();
		 rs.close();
		 sc.getConnection().close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return allItems;
	}

	}
	

}
